clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear

gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory
gen margin_victory_sq=margin_victory^2
gen treat_margin_victory_sq=treat*margin_victory_sq
	******************
	*	TABL
	*****************
	texdoc close 
	cap erase "$dir/Tables/TableF2.tex"
	texdoc init "$dir/Tables/TableF2.tex", force

	tex \begin{table}[h]
	tex \caption{Effect of electoral victory on benefits to donors (global parametric linear RD)}\label{tab:table_benefits_l}
	tex \centering
		tex \scalebox{.8}{
	tex \begin{tabular}{l H H c c c c} \hline
tex Outcome:& &&\multicolumn{2}{c}{Receive contract}&Receive contract& Runs in 2015\\
	tex & & &\multicolumn{2}{c}{(municipality)} & (outside)& \\
	tex & Non-Family &Family & Non-Family &Family  & Family &Family \\
	tex & (1) & (2) & (3) & (4)& (5) & (6)\\ \hline
	tex & & & & & &\\
	
	*preserve
	*drop contract
	*rename got_any contract
	
	*Model 1
	foreach x in   contract{
	
		*No Family
		*Regressions
		quietly: regress nf`x' treat treat_margin_victory_sq margin_victory_sq treat_margin_victory margin_victory, vce(cluster muni_code)
		quietly sum nf`x' if e(sample)
			local nfmean_`x' : di %5.3f r(mean)
			local nfsd_`x' : di %5.3f r(sd) 		
		
		regress nf`x' treat treat_margin_victory_sq margin_victory_sq treat_margin_victory margin_victory, vce(cluster muni_code)

		local nfN_`x' : di %5.0f e(N)
		local nfR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local nfb1_`x' : di %5.3f b[1,1]
		local nfse1_`x' : di %5.3f sqrt(v[1,1])
		local nfp_v_`x' :di %5.3f res[4,1]
		local nfuci_`x': di %5.3f res[6,1]
		local nflci_`x': di %5.3f res[5,1]
		

	}

	
	foreach x in  contract got_above_ext runs_any{
	
		*No Family
		*Regressions
		quietly sum f`x' 
			local fmean_`x' : di %5.3f r(mean)
			local fsd_`x' : di %5.3f r(sd) 		
		
		regress f`x' treat treat_margin_victory_sq margin_victory_sq treat_margin_victory margin_victory, vce(cluster muni_code)

		local fN_`x' : di %5.0f e(N)
		local fR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`x' : di %5.3f b[1,1]
		local fse1_`x' : di %5.3f sqrt(v[1,1])
		local fp_v_`x' :di %5.3f res[4,1]
		local fuci_`x': di %5.3f res[6,1]
		local flci_`x': di %5.3f res[5,1]
		
	}
	
	

	*Continue table
	tex Electoral victory & `nfb1_total_cont_num_d' &  `fb1_total_cont_num_d'& `nfb1_contract' & `fb1_contract' & `fb1_got_above_ext' & `fb1_runs_any' \\

	tex \ \ \ \  p-value & `nfp_v_total_cont_num_d' &  `fp_v_total_cont_num_d' & `nfp_v_contract' & `fp_v_contract' & `fp_v_got_above_ext' & `fp_v_runs_any' \\
	
	
	tex \ \ \ \ CI 95\%  & [`nflci_total_cont_num_d',`nflci_total_cont_num_d'] & [`flci_total_cont_num_d',`fuci_total_cont_num_d'] &  [`nflci_contract',`nfuci_contract'] & [`flci_contract',`fuci_contract'] & [`flci_got_above_ext',`fuci_got_above_ext'] & [`flci_runs_any',`fuci_runs_any']\\
	tex & & & \\
	
	tex Observations &`nfN_total_cont_num_d' &  `fN_total_cont_num_d' & `nfN_contract'& `fN_contract' & `fN_got_above_ext' & `fN_runs_any' \\
	tex Mean & `nfmean_total_cont_num_d' &`fmean_total_cont_num_d'  &  `nfmean_contract' & `fmean_contract' & `fmean_got_above_ext' & `fmean_runs_any' \\ \hline
	tex \end{tabular}
	tex }
	tex \parbox{160mm}{ 
	tex \footnotesize{OLS estimates of average treatment effects at the cutoff. Controls include the interaction of the treatment with running variable and running variable, interaction of the treatment with the squared running variable, and the running variable squared. P-values and 95\% robust confidence intervals with clustering at the municipality level.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 

	